import axios from "axios";

export const FetchCamera = () =>{
    const Ystoken = async (token) =>{
        try {
            const response = await axios.get('http://127.0.0.1:8085/camera/GetToken', {
                headers: {
                    Authorization: `Bearer ${token}`,
                },
            });
            return await response.data;
        } catch (error) {
            console.error('获取摄像头token失败:', error);
            return null;
        }
    };
    const FetchUrl = async (token) =>{
        try {
            const response = await axios.get('http://127.0.0.1:8085/camera/FetchUrl', {
                headers: {
                    Authorization: `Bearer ${token}`,
                },
            });
            return await response.data;
        } catch (error) {
            console.error('获取历史抓拍数据失败:', error);
            return null;
        }
    };
    return { Ystoken,FetchUrl};
};
export const FetchSensor = () =>{
    const Sensor = async (token) =>{
        try {
            const response = await axios.get('http://127.0.0.1:8085/sensor/SensorLastInfo', {
                headers: {
                    Authorization: `Bearer ${token}`,
                },
            });
            return await response.data;
        } catch (error) {
            console.error('获取传感器数据失败:', error);
            return null;
        }
    };
    const FetchList = async (token) =>{
        try {
            const response = await axios.get('http://127.0.0.1:8085/sensor/InfoList', {
                headers: {
                    Authorization: `Bearer ${token}`,
                },
            });
            return await response.data;
        } catch (error) {
            console.error('获取传感器数据列表失败:', error);
            return null;
        }
    };
    return { Sensor,FetchList}
};
export const FetchUser = () =>{
    const Login = async (username, password) => {
        try {
            const response = await axios.post('http://127.0.0.1:8085/user/login', null, {
                params: {
                    username,
                    password,
                },
            });
            return await response.data; // 直接返回响应数据
        } catch (error) {
            console.error('登录失败:', error);
            return null; // 处理错误
        }
    };
    const VerifyToken = async (token) => {
        return axios.get('http://127.0.0.1:8085/user/verify', {
            headers: {
                Authorization: `Bearer ${token}`, // 使用 Bearer 方案
            },
        })
            .then(response => response.data) // 返回响应数据
            .catch(error => {
                console.error('验证 token 失败:', error);
                return null; // 返回 null 表示验证失败
            });
    };
    return { Login,VerifyToken}
};
